package com.financing.platform.web.api;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ProductVideo;
import com.financing.platform.service.IProductVideoService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;

@Controller("FpProductVideoAction")
@RequestMapping("/api/productVideo")
@Scope("prototype")
public class FpProductVideoAction extends BaseAction{
	
	@Autowired
	IProductVideoService productVideoService;
	
	/**
	 * 查询产品视频信息
	 * @param request
	 * @param response
	 * @param productVideo
	 * @return
	 */
	@RequestMapping("/queryProductVideoInfo.shtml")
	@ResponseBody
	public ResultMap queryProductVideoInfo(HttpServletRequest request,
			HttpServletResponse response,ProductVideo productVideo) {
		try{
			if(StringUtil.isNullOrEmptyStr(productVideo.getProductType())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			if(StringUtil.isNullOrEmptyStr(productVideo.getProductId().toString())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			resultMap.setAttribute("list",productVideoService.queryProductVideo(productVideo));
			
			//从和讯那里获取所有视频列表
			URL url = new URL("http://v.ner98.com/index.php?getvideolist=sfa");
			InputStream is= url.openConnection().getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			StringBuilder sb =  new StringBuilder();
			String s;
			while(( s = br.readLine())!=null){
				sb.append(s);
			}
			resultMap.setAttribute("allList", sb);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("addAction error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			return resultMap;
		}
		return resultMap;
	}
	
	/**
	 * 更新产品视频信息
	 * @param request
	 * @param response
	 * @param productVideo
	 * @return
	 */
	@RequestMapping("/updateProductVideoInfo.shtml")
	@ResponseBody
	public ResultMap updateProductVideoInfo(HttpServletRequest request,
			HttpServletResponse response,ProductVideo productVideo) {
		try{
			if(StringUtil.isNullOrEmptyStr(productVideo.getProductType())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			if(productVideo.getProductId() == null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			if(StringUtil.isNullOrEmptyStr(productVideo.getVideoContent())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			if(StringUtil.isNullOrEmptyStr(productVideo.getVideoUrl())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			if(StringUtil.isNullOrEmptyStr(productVideo.getVideoOtherId())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			DTOdateSet.setCreateInfo(productVideo, request.getSession());
			DTOdateSet.setUpdateInfo(productVideo, request.getSession());
			productVideoService.updateProductVideo(productVideo);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("addAction error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
}
